home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / QuickTime / Show Movie / Sources / Show Movie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-20  |  6.5 KB  |  305 lines  |  [TEXT/MMCC]

  1. /*
  2.   File:            Show Movie.c
  3.   Contains:         main and event loop
  4.   Written by:    Jason Hodges-Harris & Don Swatman
  5.   Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  6. */
  7.  
  8. //==============================================
  9. //    Show Movie
  10. //
  11. //  Allows you to open and display movies
  12. //  in a window.
  13. //
  14. //==============================================
  15.  
  16. // Mac Headers
  17. #include <Types.h>
  18. #include <Memory.h>
  19. #include <Quickdraw.h>
  20. #include <Fonts.h>
  21. #include <Events.h>
  22. #include <Menus.h>
  23. #include <Windows.h>
  24. #include <TextEdit.h>
  25. #include <Dialogs.h>
  26. #include <OSUtils.h>
  27. #include <ToolUtils.h>
  28. #include <SegLoad.h>
  29. #include <Movies.h>
  30. #include <DiskInit.h>
  31.  
  32. // Show Movie Headers
  33. #include "MenuStuff.h"
  34. #include "WindStuff.h"
  35. #include "MovieStuff.h"
  36. #include "MoviePrefs.h"
  37.  
  38. //----------------------------------------------
  39. // Prototypes
  40. //----------------------------------------------
  41.  
  42.     // Inititalization
  43. OSErr InitMacStuff(void);
  44. void InitGlobals(void);
  45. void KillGlobals(void);
  46.     
  47.     // Events
  48. void EventLoop (void);
  49.  
  50.  
  51. //==============================================
  52. //  main
  53. //==============================================
  54.  
  55. void main(void)
  56. {
  57.     OSErr theErr;
  58.     
  59.     theErr = InitMacStuff();
  60.     if (!theErr)
  61.     {
  62.         InitGlobals();    // initialise globals
  63.         
  64.         EventLoop();      // Call the main event loop.
  65.  
  66.         KillGlobals();    // Tear down any globals
  67.  
  68.         ExitToShell();    // Quit the application.
  69.  
  70.     }
  71.     
  72. }
  73.  
  74.  
  75. //==============================================
  76. // Initialisation Stuff
  77. //==============================================
  78.  
  79. //----------------------------------------------
  80. // InitMacStuff
  81. //
  82. // Initialise the Mac Tool Box and set up the memory
  83. //----------------------------------------------
  84.  
  85. OSErr InitMacStuff(void)
  86. {
  87.     long     *appSize;
  88.     OSErr          theErr = noErr;
  89.         
  90. // This decreases the application heap by 16k
  91. //       (which increases the stack by 16k.)
  92.     appSize = (long*)(GetApplLimit());
  93.     SetApplLimit (appSize -16384);
  94.  
  95. // Expand the heap so code segments load at the top.
  96.     MaxApplZone();
  97.  
  98. // allocate more master pointers
  99.     MoreMasters();
  100.     MoreMasters();
  101.  
  102. // ****** Init Mac toolbox ******
  103.     InitGraf (&qd.thePort);
  104.     InitFonts();
  105.     InitWindows();
  106.     InitMenus();
  107.     TEInit();
  108.     InitDialogs(0L);
  109.     InitCursor();
  110.  
  111. // initialise movie toolbox
  112.     theErr = EnterMovies();
  113.     if (theErr)
  114.     {
  115.         DebugStr("\pError in initialising movie toolbox.");
  116.     }
  117.     return(theErr);
  118. }
  119.  
  120. //----------------------------------------------
  121. // InitGlobals
  122. //
  123. // Initialise all "Show Movie" globals
  124. //----------------------------------------------
  125.  
  126. void InitGlobals(void)
  127. {
  128.     short windCount;
  129.     
  130. // init the windows list
  131.     for (windCount = 0; windCount < kMaxWindows; windCount++)
  132.         gTheWinds[windCount] = nil;
  133.  
  134.     gDone = false; // Set the done flag
  135.  
  136.     InitMovieGlobals();  // init the movie globals
  137.     InitMoviePrefs();    // init the movie prefs
  138.     MenuBarInit();       // init menubar
  139. }
  140.  
  141. //----------------------------------------------
  142. // KillGlobals
  143. //
  144. // Kills all "Show Window" Globals and closes the windows
  145. //----------------------------------------------
  146.  
  147. void KillGlobals(void)
  148. {
  149.     CloseAllWindows();
  150.     KillMovieGlobals();
  151.     KillMoviePrefs();
  152. }
  153.  
  154. //==============================================
  155. //  Event Loop and event handling 
  156. //==============================================
  157.  
  158. //----------------------------------------------
  159. //  HandleMouseDown
  160. //
  161. // This looks after a mouse down. It works out where
  162. // the mouse has clicked and then deals with it 
  163. // apropriately
  164. //----------------------------------------------
  165.  
  166. void HandleMouseDown(EventRecord *eventPtr);
  167. void HandleMouseDown(EventRecord *eventPtr)
  168. {
  169.     WindowPtr    window;
  170.     short        thePart;
  171.     long        menuChoice;
  172.     
  173. // Find out where the user has clicked
  174.     thePart=FindWindow (eventPtr->where,&window);
  175.     switch (thePart)
  176.     {
  177. // Handle a click in the menu bar
  178.         case inMenuBar:
  179.             menuChoice = MenuSelect (eventPtr->where); // Displays menus and returns a selection 
  180.             DoMenuCommand(menuChoice);                 // Respond to the menu choice
  181.             break;
  182.  
  183. // In a desk accesory window, so let the system deal with it
  184.         case inSysWindow:
  185.             SystemClick(eventPtr,window);
  186.             break;
  187.  
  188. // In the title bar (drag region)
  189. //    so drag the window around
  190.         case inDrag:
  191.             DragSelWind(window,eventPtr->where);
  192.  
  193. // In the go away button
  194. //    need to tracks the mouse and close the window if released in go away button
  195.         case inGoAway:
  196.             DoGoAwayWind(window,eventPtr->where);  
  197.             break;
  198.  
  199. // In the windows content area
  200.         case inContent:
  201.             {
  202.     // If this is not the front window, then bring the window to the front
  203.                 if (window!=FrontWindow())
  204.                 {
  205.                     SelectWindow(window);
  206.                     break;
  207.                 }
  208.             }
  209.         break;
  210.     }
  211. }
  212.  
  213.  
  214. //----------------------------------------------
  215. // DoDiskEvt
  216. //
  217. //   Handle disk inserted event
  218. //----------------------------------------------
  219.  
  220. void DoDiskEvt(EventRecord *eventPtr);
  221. void DoDiskEvt(EventRecord *eventPtr)
  222. {
  223.     short    errResult;
  224.     Point    errPoint;
  225.  
  226.     if((HiWord(eventPtr->message)!=noErr))
  227.     {
  228.         SetPt(&errPoint,100,100);
  229.         errResult=DIBadMount(errPoint,eventPtr->message);
  230.     }
  231. }
  232.  
  233. //----------------------------------------------
  234. // Event Loop
  235. //----------------------------------------------
  236.  
  237. void EventLoop (void)
  238. {
  239.     EventRecord    event;       // The event returned
  240.     char          theChar;     // what character the user pressed
  241.     long        menuChoice;  // Menu & item the user selected
  242.  
  243.     DoAdjustMenus();  // Adjust appropriately
  244.     
  245. // Keep going until the user decides to quit (i.e. gDone = true)
  246.     while (!gDone)
  247.     {
  248. // Change the cursor to an arrow
  249.         SetCursor( &(qd.arrow));
  250.  
  251. // Receive every event from the system
  252.         WaitNextEvent (everyEvent,&event,0,nil);
  253.  
  254. // Do anything that the movies needs to do, including clicking
  255. // in the movie control
  256.         if (!ServiceMovieTasks( FrontWindow(), &event ))
  257.         {
  258.  
  259. // If event still needs to handled then do switch
  260.             switch (event.what)
  261.             {
  262.     // null event, do nothing
  263.                 case nullEvent:
  264.                     break;
  265.  
  266.     // user has clicked so deal with it
  267.                 case mouseDown:
  268.                     HandleMouseDown(&event);
  269.                     break;
  270.  
  271.     // user has pressed a key
  272.                 case keyDown:
  273.                 case autoKey:
  274.  
  275.             // Extract the key pressed
  276.                     theChar=event.message & charCodeMask; 
  277.  
  278.             // If the Command key was down then compare it pass it
  279.                     if (event.modifiers & cmdKey)
  280.                     {
  281.                         menuChoice = MenuKey (theChar);   // Find out what menu was selected by the keypress
  282.                         DoMenuCommand( menuChoice  );     // Respond to the menu choice
  283.                     }
  284.                     break;
  285.  
  286.     // window needs to be updated, or redrawn
  287.                 case updateEvt:
  288.                     DoWindUpdate((WindowPtr)(event.message));
  289.                     break;
  290.  
  291.     // disk inserted event
  292.                 case diskEvt:
  293.                     DoDiskEvt(&event);
  294.                     break;
  295.  
  296.     // Operating system event
  297.                 case osEvt:
  298.                     break;
  299.             }
  300.         }
  301.         
  302.     }
  303.  
  304. }
  305.